local boyinghun = fk.CreateSkill{
    name = "boyinghun",
}

local U = require "packages/jiudianbo/utility/utility"

boyinghun:addEffect(fk.EventPhaseStart, {
    anim_type = "drawcard",
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and 
               player:hasSkill(boyinghun.name) and 
               player.phase == Player.Start and
               #player.room.players > 0
    end,
    on_cost = function(self, event, target, player, data)
        if not player then return false end
        
        -- 改为获取当前体力值（player.hp）而非体力上限（player.maxHp）
        local to = player.room:askToChoosePlayers(player, {
            min_num = 1,
            max_num = 1,
            targets = player.room.players,
            skill_name = boyinghun.name,
            prompt = "#boyinghun-choose:::" .. U.ConvertNumber(player.hp),
            cancelable = true,
        })
        
        if #to > 0 then
            event:setCostData(self, {tos = to})
            return true
        end
        return false
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        -- 改为获取当前体力值（player.hp）而非体力上限（player.maxHp）
        local n = player.hp
        
        local choice = room:askToChoice(player, {
            choices = {
                "#boyinghun-draw:::" .. U.ConvertNumber(n),
                "#boyinghun-discard:::" .. U.ConvertNumber(2)
            },
            skill_name = boyinghun.name
        })
        
        if choice:startsWith("#boyinghun-draw") then
            player:broadcastSkillInvoke(boyinghun.name, 1)
            room:notifySkillInvoked(player, boyinghun.name, "support")
            to:drawCards(n, boyinghun.name)
            if to:isAlive() then
                room:askToDiscard(to, {
                    skill_name = boyinghun.name,
                    cancelable = false,
                    min_num = 2,
                    max_num = 2,
                    include_equip = true
                })
            end
        else
            player:broadcastSkillInvoke(boyinghun.name, 2)
            room:notifySkillInvoked(player, boyinghun.name, "control")
            to:drawCards(2, boyinghun.name)
            if to:isAlive() then
                room:askToDiscard(to, {
                    skill_name = boyinghun.name,
                    cancelable = false,
                    min_num = n,
                    max_num = n,
                    include_equip = true
                })
            end
        end
    end,
})

Fk:loadTranslationTable{
    ["boyinghun"] = "英魂",
    -- 技能描述改为“X为你体力值”
    [":boyinghun"] = "准备阶段，你可选择一名角色并选择一项：1.令其摸X张牌，然后弃置2张牌；2.令其摸2张牌，然后弃置X张牌（X为你体力值）。",
    ["#boyinghun-choose"] = "英魂：你可以令一名角色：摸%arg张牌然后弃置2张牌，或摸2张牌然后弃置%arg张牌",
    ["#boyinghun-draw"] = "摸%arg张牌，弃置2张牌",
    ["#boyinghun-discard"] = "摸2张牌，弃置%arg张牌",
    ["$boyinghun1"] = "不见狼居胥，何妨马革裹尸。",
    ["$boyinghun2"] = "既无功，不受禄。",
}

return boyinghun
